package io.gitee.zhangbinhub.test.reactive.controller

import io.gitee.zhangbinhub.acp.boot.component.FileDownLoadHandle
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.ConstraintViolationException
import jakarta.validation.constraints.Max
import jakarta.validation.constraints.Min
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.http.server.reactive.ServerHttpResponse
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono

/**
 * @since JDK 17
 */
@RestController
@Validated
@Tag(name = "测试接口")
class ReactiveController(
    private val fileDownLoadHandle: FileDownLoadHandle
) {

    @Operation(summary = "测试异常1")
    @GetMapping("/test/exception/1", produces = [(MediaType.APPLICATION_JSON_VALUE)])
    @Throws(WebException::class)
    fun testEx1(@RequestParam("name") name: String): Mono<Any?> =
        throw WebException(HttpStatus.CONFLICT, "conflict")

    @Operation(summary = "测试异常2")
    @GetMapping("/test/exception/2", produces = [(MediaType.APPLICATION_JSON_VALUE)])
    @Throws(WebException::class)
    fun testEx2(
        @Max(value = 5, message = "number1不能大于5") @RequestParam("number1") number1: Int,
        @Min(value = 5, message = "number2不能小于5") @RequestParam("number2") number2: Int
    ): Mono<Any?> =
        throw WebException("就是异常了")

    @Operation(summary = "测试异常3")
    @GetMapping("/test/exception/3", produces = [(MediaType.APPLICATION_JSON_VALUE)])
    @Throws(ConstraintViolationException::class)
    fun testEx3(@RequestParam("name") name: String): Mono<String> =
        Mono.just("{\"name\":\"exception 3\"}")

    @GetMapping("/download")
    @Throws(WebException::class)
    fun download(
        request: ServerHttpRequest,
        response: ServerHttpResponse
    ): Mono<Void> {
        val filePath = "D:\\test\\out1.xls"
        return fileDownLoadHandle.downLoadFile(request, response, filePath, listOf(filePath))
    }
}